package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService_Lizekai;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController_Lizekai {
    @Reference
    private RoleService_Lizekai roleService;
    @RequestMapping("findMenuAll")
    public Result findMenuAll(){
        try {
            List<Menu> list = roleService.findMenuAll();
            return new Result(true, "查询菜单列表成功", list);
        } catch (Exception e) {
            return new Result(false, "查询菜单列表失败");
        }
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds){
        try{
            roleService.add(role,permissionIds,menuIds);
        }catch (Exception e){
            return new Result(false, "添加角色失败");
        }
        return new Result(true,"添加角色成功");
    }
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Permission> list = roleService.findAll();
            return new Result(true, "查询权限列表成功", list);
        } catch (Exception e) {
            return new Result(false,"查询权限列表失败");
        }
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.findPage(queryPageBean);
        return pageResult;
    }
    @RequestMapping("/findById")
    public Result findById(int id){
        try{
            Role role = roleService.findById(id);
            return  new Result(true,"查询角色信息成功",role);
        }catch (Exception e) {
            return new Result(false,"查询角色信息失败");
        }
    }
    @RequestMapping("/findPermissionIdByRoleId")
    public Result findPermissionIdByRoleId(int id){
        try{
            List<Integer> permissionIds = roleService.findPermissionIdByRoleId(id);
            return  new Result(true,"查询角色对应权限信息成功",permissionIds);
        }catch (Exception e) {
            return new Result(false,"查询角色对应权限信息失败");
        }
    }
    @RequestMapping("/findMenuIdByRoleId")
    public Result findMenuIdByRoleId(int id){
        try{
            List<Integer> menuIds = roleService.findMenuIdByRoleId(id);
            return  new Result(true,"查询角色对应菜单信息成功",menuIds);
        }catch (Exception e) {
            return new Result(false,"查询角色对应菜单信息失败");
        }
    }
    @RequestMapping("/delete")
    public Result delete(int id){
            return roleService.deleteById(id);
    }
    @RequestMapping("/update")
    public Result update(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.update(role,permissionIds,menuIds);
        }catch (Exception e) {
            return new Result(false,"编辑角色信息失败");
        }
        return new Result(true,"编辑角色信息成功");
    }
}
